<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp" %>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>

<form id="${uuid}_scriptDefForm">
	<input type="hidden" name="id">
	<input type="hidden" name="queryDefId">
	<shiro:hasPermission name="QUERY_DYN_SCRIPT">
	<button type="button" class="btn btn-primary"  id="saveScriptBt">保存</button>
	</shiro:hasPermission>
	<div class="am-margin-top-sm">
		<textarea id="scriptView" style="height: 220px;width: 100%" name="scriptContentStr"></textarea>
	</div>
</form>

<script type="text/javascript">
$(function(){
	var uuid = '${uuid}';
	var queryDefId='${param.queryDefId}';
	var _scriptDefForm=$("#"+uuid+"_scriptDefForm");
	
		
		$("#saveScriptBt").click(function(){
			if(_scriptDefForm.queryDefId){
				_scriptDefForm.find("input[name=queryDefId]").val(_scriptDefForm.queryDefId);
				
				var formObj=webUtils.formToObj(_scriptDefForm);
				$.post(ctx+"/queryDefAction/saveScriptDef.do",{datas:Base64.encode(JSON.stringify(formObj))},function(data){
					if (data.success) {
                        webUtils.alert("提交成功");
                        webUtils.fullForm({
            				form:_scriptDefForm,
            				data:data.obj
            			});
                    } else {
                        webUtils.error(data.msg);
                    }
				},"json");
			}else{
				webUtils.alert("请选择对应的查询定义。");
			}
			
		});
		
		
		function loadScriptDefForm(){
			_scriptDefForm.queryDefId=queryDefId;
			$.getJSON(ctx+"/queryDefAction/findScriptDefByQueryDefId.do",{
				queryDefId:queryDefId
			},function(data){
				var s=$.extend({
					id:"",
					queryDefId:"",
					scriptContentStr:""
				},data);
				
				$("#scriptView").val(s.scriptContentStr);
				webUtils.fullForm({
					form:_scriptDefForm,
					data:s
				});
			});
		}
		
		loadScriptDefForm();
});
</script>